//
//  SegmentView.swift
//  FM
//
//  Created by x on 2017/9/25.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

protocol SegmentViewDelegate:NSObjectProtocol {
    
    /// 选择哪个模块
    ///
    /// - Parameter index: <#index description#>
    func btuWillClick(index:Int)
}

class SegmentView: UIView {
    
    /// 保存选中按钮
    var selectorBtu:UIButton?
    
    /// 代理对象
    weak var choiseDelegate:SegmentViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.9, alpha: 0.8)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 初始化
    func setupUI() -> Void {
        //1.添加子控件
        addSubview(essenceBtu)
        addSubview(newBtu)
        addSubview(markLable)
        addSubview(slideLable)
        
        //2.布局子控件
        let dict = ["essenceBtu":essenceBtu,"newBtu":newBtu,"markLable":markLable] as [String:Any]
        
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        
        let metrics = ["btuW":(bounds.width-80)/2,"btuH":bounds.height]
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[essenceBtu(btuW)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: metrics, views:  dict)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[essenceBtu(btuH)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: metrics, views:  dict)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[essenceBtu]-20-[markLable(1)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-15-[markLable(10)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[markLable]-20-[newBtu(btuW)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: metrics, views:  dict)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[newBtu(btuH)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: metrics, views:  dict)
        
        addConstraints(cons)
        
        slideLable.translatesAutoresizingMaskIntoConstraints = true
        slideLable.frame = CGRect.init(x: 20, y: bounds.height-1, width: (bounds.width-80)/2, height: 1)
        
        //3.设置选中按钮
        essenceBtu.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        selectorBtu = essenceBtu
    }
    
    /// 按钮点击事件
    ///
    /// - Parameter btu: <#btu description#>
    func btuClick(btu:UIButton) -> Void {
        
        //1.改变按钮的字体大小
        selectorBtu?.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btu.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        selectorBtu = btu
        
        //2.改变滑动lable的位置
        if btu.tag == 1 {
            UIView.animate(withDuration: 0.3, animations: {
                self.slideLable.frame.origin = CGPoint(x: 20, y: self.bounds.height-1)
            })
        }else {
            UIView.animate(withDuration: 0.3, animations: { 
                self.slideLable.frame.origin = CGPoint(x: (self.bounds.width-80)/2+60, y: self.bounds.height-1)
            })
        }
        
        //3.代理方法调用
        choiseDelegate?.btuWillClick(index: btu.tag)
        
    }
    
    //MARK: -- 懒加载

    /// 精华按钮
    private lazy var essenceBtu:UIButton = UIButton.buttonWith(title: "精华", titleColor: UIColor.RGBColor(r: 234, g: 110, b: 68), fontSize: 12, tag:1, target: self, action: #selector(btuClick(btu:)))
    
    /// 最新按钮
    private lazy var newBtu:UIButton = UIButton.buttonWith(title: "最新", titleColor: UIColor.RGBColor(r: 234, g: 110, b: 68), fontSize: 12, tag:2, target: self, action: #selector(btuClick(btu:)))
    
    /// 背景lable
    private lazy var markLable:UILabel = UILabel.lableWith(backgroundColor: UIColor.RGBColor(r: 234, g: 110, b: 68))
    
    /// 滑动lable
    private lazy var slideLable:UILabel = UILabel.lableWith(backgroundColor: UIColor.RGBColor(r: 234, g: 110, b: 68))
    
}
